Written by Peter Mather
See: http://www.thebackshed.com/forum/forum_posts.asp?TID=7826

======================================================================================

The attached code (tested on 4.7b23) allows you to read the ADC but optionally
use the external reference voltage inputs to improve accuracy. The code does not
run on the MM+ (MX470). Each call to the readADC routine takes nine readings and
averages the last 8 to improve accuracy.

There are 4 modes of operation of the ADC:
mode=0, Vref+ is AVdd and Vref- is AVss
mode=1, Vref+ external and Vref- is AVss
mode=2, Vref+ AVdd and Vref- external
mode=3, both Vref+ and Vref- are external

mode 0 is the normal way it operates in the Micromite Firmware. Modes 1-3 require
reference voltages applied to one or both external inputs.

28-pin VREF+ is on pin 2, VREF- is on pin 3
44-pin VREF+ is on pin 19, VREF- is on pin 20

The readADC routine takes two parameters, the pin number to read and the mode.
All relevant pins (pin to read and reference pins) must previously be set as
analogue inputs using SETPIN n,AIN

The routine returns the actual ADC count (0-1023), you then need to convert
this to a voltage based on the actual values of Vref+ and/or Vref- and/or
AVdd as applicable.

So if Vref+ is 2.048V and Vref- is 1.024V and readADC returns 255 in mode-3
then the voltage is 1.024+ 255/1023*(2.048-1.024) = 1.279V

Example program:
======================================================================================

setpin 19,ain
SETPIN 20,AIN
SETPIN 21,AIN
do
? readADC(21,3)
pause 1000
loop
end

'
'  Function readADC
'
'  Parameters:
'  pin number, must be a valid analogue input pin that has been set up with SETPIN n,AIN
'  conversion mode
'     mode=0, A/D Voltage reference configuration Vref+ is AVdd and Vref- is AVss
'     mode=1, A/D Voltage reference configuration Vref+ external and Vref- is AVss
'     mode=2, A/D Voltage reference configuration Vref+ AVdd and Vref- external
'     mode=3, A/D Voltage reference configuration both Vref+ and Vref- are external
'
'  Pin Usage
'     28-pin VREF+ is on pin 2, VREF- is on pin 3
'     44-pin VREF+ is on pin 19, VREF- is on pin 20
'
'
'  NB reference pins must be previously set as analogue inputs using SETPIN refpin,AIN
'
CFunction readADC
     00000000
     27bdffc0 afbf003c afbe0038 afb70034 afb60030 afb5002c afb40028 afb30024
     afb20020 afb1001c afb00018 8c820000 8ca40000 24050001 10850007 24032000
     24050002 10850004 24034000 38840003 24036000 0004180b 3c04bf81 8c85f220
     7ca5d800 3c040661 24840053 10a40007 3c04bf81 8c85f220 7ca5d800 3c040660
     24840053 14a4002c 2404000a 24040002 10440007 24040003 1044000b 24040004
     1044000d 3c120002 10000002 3c12000e 00009021 24040005 5044000c 3c120003
     10000003 24040006 3c120001 24040006 5044000b 3c120004 24040007 5044000d
     3c120005 10000002 24040017 24040017 5044000d 3c12000c 10000002 24040018
     24040018 50440044 3c12000b 10000002 24040019 24040019 5044003f 3c12000a
     10000002 3842001a 3842001a 3c040009 10000039 0082900a 10440007 2404000b
     1044000b 2404000e 1044000d 3c12000a 10000002 3c12000e 3c12000c 2404000f
     5044000c 3c120009 10000003 24040013 3c12000b 24040013 5044000b 00009021
     24040014 5044000d 3c120001 10000002 24040015 24040015 5044000d 3c120002
     10000002 24040016 24040016 5044000d 3c120003 10000002 24040017 24040017
     5044000d 3c120004 10000002 24040018 24040018 5044000d 3c120005 10000002
     24040019 24040019 5044000b 3c120006 10000002 2404001a 2404001a 50440006
     3c120007 10000002 3842001b 3842001b 3c040008 0082900a 3c041000 3c02bf88
     ac441064 3c02bf88 ac401068 34048000 3c02bf81 ac449004 24100001 00008821
     3c13bf81 3c14bf88 3c15bf88 3c16bf81 3c17ffff 3c1ebf81 34630003 10000002
     afa30010 26100001 ae729040 ae806118 aea06108 aed79050 34028f00 afc29020
     8fa20010 3c03bf81 ac629010 240200e4 3c03bf81 ac629000 34028000 3c03bf81
     ac629008 3c04bf88 3c031000 8c821030 00431024 1040fffd 3c02bf81 8c429070
     02228821 24020001 12020020 3c031000 3c02bf88 ac431034 3c029d00 8c420004
     0040f809 24040064 2e020009 1440ffde 26100001 3c031000 3c02bf88 ac431064
     3c02bf88 ac401068 34038000 3c02bf81 ac439004 001110c2 00001821 8fbf003c
     8fbe0038 8fb70034 8fb60030 8fb5002c 8fb40028 8fb30024 8fb20020 8fb1001c
     8fb00018 03e00008 27bd0040 3c02bf88 ac431034 3c029d00 8c420004 0040f809
     24040064 1000ffbf 00008821
End CFunction   'MIPS32 M4K


